package br.usp.ime.protoc.client.view;

import com.google.gwt.user.client.ui.Composite;
import com.gwtext.client.core.Connection;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.core.Position;
import com.gwtext.client.data.FieldDef;
import com.gwtext.client.data.RecordDef;
import com.gwtext.client.data.StringFieldDef;
import com.gwtext.client.data.XmlReader;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.FieldSet;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.form.VType;

public class PacientEditView extends Composite {
	private final Panel panel = new Panel();

	public PacientEditView(int PacientID) {

		// setup form data reader
		RecordDef recordDef = new RecordDef(new FieldDef[] {
				new StringFieldDef("nome", "nome"),
				new StringFieldDef("cpf", "cpf"), new StringFieldDef("email"),
				new StringFieldDef("fone") });

		final XmlReader reader = new XmlReader("paciente", recordDef);
		reader.setSuccess("@success");

		// setup error reader to process from submit response from server
		RecordDef errorRecordDef = new RecordDef(new FieldDef[] {
				new StringFieldDef("id"), new StringFieldDef("msg") });

		XmlReader errorReader = new XmlReader("field", errorRecordDef);
		errorReader.setSuccess("@success");

		final FormPanel formPanel = new FormPanel(Position.RIGHT);
		formPanel.setFrame(true);
		formPanel.setTitle("XML Form");
		//formPanel.setWidth(400);
		formPanel.setLabelWidth(75);
		// set reader and error reader
		formPanel.setReader(reader);
		formPanel.setErrorReader(errorReader);

		// add some fields
		FieldSet fieldSet = new FieldSet("Paciente");
		fieldSet.add(new TextField("Nome", "nome", 190));
		fieldSet.add(new TextField("CPF", "cpf", 190));
		fieldSet.add(new TextField("Telefone", "fone", 190));
		TextField email = new TextField("Email", "email", 190);
		email.setVtype(VType.EMAIL);
		fieldSet.add(email);

		formPanel.add(fieldSet);

		final Button submitBtn = new Button("Submit",
				new ButtonListenerAdapter() {
					public void onClick(Button button, EventObject e) {
						formPanel.getForm().submit("data/xml-errors.xml", null,
								Connection.GET, "Saving Data...", false);
					}
				});

		Button loadBtn = new Button("Load", new ButtonListenerAdapter() {
			public void onClick(Button button, EventObject e) {
				formPanel.getForm().load("data/xml-form.xml", null,
						Connection.GET, "Loading...");
				submitBtn.enable();
			}
		});
		formPanel.addButton(loadBtn);
		formPanel.addButton(submitBtn);
		panel.add(formPanel);
		this.initWidget(panel);
	}
}
